package com.lgx.mall.core.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.TreeUtils;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.mall.core.dao.ICategoryDao;
import com.lgx.mall.core.entity.Category;

/**
 * 
 * @author acer
 *
 */
@Component("categoryDao")
public class CategoryDao extends GeneralDao implements ICategoryDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}
	
	@Transactional
	public Category createCategory(Category category) throws DataException {
		this.getHibernateTemplate().save(category);
		log.info("[创建商品类型分类]:"+category.getId());
		return category;
	}

	@Transactional
	public Category updateCategory(Category category) throws DataException {
		this.getHibernateTemplate().update(category);
		log.info("[更新商品类型分类]:"+category.getId());
		return category;
	}

	@Transactional
	public void deleteCategory(Category category) throws DataException {
		log.info("[删除商品类型分类]:"+category.getId());
		this.getHibernateTemplate().delete(category);
	}

	public void deleteCategory(long id) throws DataException {
		this.deleteCategory(this.findCategoryById(id));
		log.info("[删除商品类型分类]:"+id);
	}

	public Category findCategoryById(long id) {
		return this.getHibernateTemplate().get(Category.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Category> findCategoryList(String sql) {
		return this.getHibernateTemplate().find(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> findBrandCategoryList(long brandId){
//		String sql =" SELECT RE.* "+
//		"   FROM TS_RESOURCE RE "+
//		"  INNER JOIN TS_PERMISSION PE ON RE.PERMISSION_ID = PE.ID "+
//		"  INNER JOIN TS_ROLE_PERMISSION RP ON PE.ID = RP.PERMISSION_ID "+
//		"  INNER JOIN TS_ROLE RO ON RP.ROLE_ID = RO.ID "+
//		"  INNER JOIN TS_USER_ROLE UR ON RO.ID = UR.ROLE_ID "+
//		"  INNER JOIN TS_USER U ON UR.USER_ID = U.ID "+
//		"  WHERE RE.ISDELETE = '0' "+
//		"    AND RE.PARENT IS NOT NULL "+
//		"    AND PE.ISDELETE = '0' "+
//		"    AND U.ID =  "+ brandId +
//		"  ORDER BY RE.LAYERORDER ";
		String sql = "";
		return (List<Category>) baseDao.queryJdbcForList(sql, Category.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> findCategoryList(){
		String sql =" SELECT CA.* "+
				"   FROM TM_CATEGORY CA "+
				"  ORDER BY CA.LAYERORDER ";
		List<Category> ca = (List<Category>) baseDao.queryJdbcForList(sql, Category.class);
		return (List<Category>) TreeUtils.addChildren(ca, null, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<TreeModel> findCategoryListToCombotree(){
		String sql =" SELECT CA.ID,CA.NAME AS TEXT,ca.layerOrder"+
				"   FROM TM_CATEGORY CA "+
				"  ORDER BY CA.LAYERORDER ";
		List<TreeModel> ca = (List<TreeModel>) baseDao.queryJdbcForList(sql, TreeModel.class);
		return (List<TreeModel>) TreeUtils.addChildren(ca, null, null);
	}

	@SuppressWarnings("unchecked")
	public List<Category> findChildrenCategoryListById(long id){
		String sql =" SELECT CA.* "+
				"   FROM TM_CATEGORY CA "+
				"	WHERE CA.PARENT="+id+
				"  ORDER BY CA.LAYERORDER ";
		return (List<Category>) baseDao.queryJdbcForList(sql, Category.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findCategoryParentList() {
		String sql = "from Category c where c.parent is null order by c.layerOrder asc";
		return this.getHibernateTemplate().find(sql);
	}
}
